import {combineReducers} from 'redux-immutable';
import * as actions from './actions';
import Immutable from 'immutable';
import {
    GET_CURRENT_USER_PAGE_PERMISSIONS_BEGIN,
    GET_CURRENT_USER_PAGE_PERMISSIONS_SUCCESS,
    GET_CURRENT_USER_PAGE_PERMISSIONS_FAIL
} from 'Shared/actions/currentUserContext';
import {DEFAULT_QUERY_OPTION} from './constants';
const init = (state, action) => {
    switch(action.type) {
        case actions.GET_INIT_DATA_BEGIN:
            return state.merge({
                isFetching: true,
                message: '',
            });
        case actions.GET_INIT_DATA_SUCCESS:
            return state.merge({
                isFetching: false,
                brands: action.data.brands,
                dealer: action.data.dealer,
                isDealerUser: action.data.isDealerUser,
                message: action.message,
            });
        case actions.GET_INIT_DATA_FAIL:
            return state.merge({
                isFetching: false,
                message: '',
            });
        default:
            return state;
    }
};
const list = (state, action) => {
    switch(action.type) {
        case actions.GET_LIST_DATA_BEGIN:
            return state.merge({
                isFetching: true,
                hasError: '',
            });
        case actions.GET_LIST_DATA_SUCCESS:
            return state.merge({
                isFetching: false,
                total: action.data.totalElements,
                hasError: '',
                data: action.data.content
            });
        case actions.GET_LIST_DATA_FAIL:
            return state.merge({
                isFetching: false,
                total: 0,
                data: [],
                hasError: action.message,
            });
        default:
            return state;
    }
};
const appState = (state, action) => {
    switch(action.type) {
        case actions.SAVE_QUERY_CONDITION:
            return state.setIn(['queryCondition', action.name], action.value);
        case actions.RESET_QUERY_PANEL:
            return state.set('queryCondition', Immutable.fromJS(DEFAULT_QUERY_OPTION));
        case actions.GET_LIST_DATA_SUCCESS:
            return state.set('queryBySort', Immutable.fromJS(action.condition));
        case actions.SAVE_QUERY_DEALER:
            return state.set('queryDealer', action.data);
        default:
            return state;
    }
};
export const permission = (state, action) => {
    switch(action.type) {
        case GET_CURRENT_USER_PAGE_PERMISSIONS_BEGIN:
            return state.merge(Immutable.fromJS({
                isFetching: true,
                message: ''
            }));
        case GET_CURRENT_USER_PAGE_PERMISSIONS_SUCCESS:
            return state.merge(Immutable.fromJS({
                isFetching: false,
                message: '',
                data: action.data
            }));
        case GET_CURRENT_USER_PAGE_PERMISSIONS_FAIL:
            return state.merge(Immutable.fromJS({
                isFetching: false,
                message: action.message
            }));
        default:
            return state;
    }
};
const detail = (state, action) => {
    switch(action.type) {
        case actions.GET_DETAIL_BEGIN:
            return state.merge({
                isFetching: true,
                message: '',
                data: {}
            });
        case actions.GET_DETAIL_SUCCESS:
            return state.merge({
                isFetching: false,
                message: '',
                data: action.data
            });
        case actions.GET_DETAIL_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message,
                data: {}
            });
        default:
            return state;
    }
};
const domainData = combineReducers({
    init,
    permission,
    list,
    detail
});

export default combineReducers({
    domainData,
    appState,
});
